package com.stone.front.dish.service;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stone.back.dish.entity.BfCombo;
import com.stone.back.dish.entity.BfComboDish;
import com.stone.back.dish.entity.BfDish;
import com.stone.back.dish.entity.BfWeekmenu;
import com.stone.back.dish.entity.BfWeekmenuCombo;
import com.stone.common.util.DateUtil;
import com.stone.front.dish.dao.IWaresFrontDao;

/**
 * 商品管理
 * @author Stone P
 *
 */
@Service
public class WaresFrontService implements IWaresFrontService {
	@Autowired
	private IWaresFrontDao waresFrontDao;
	
	/**
	 * 获取本周菜单
	 * @param date		当天所在周的第一天日期
	 * @return
	 */
	@Override
	public BfWeekmenu selectWeekMenu() {
		Calendar nowWeek = Calendar.getInstance();
		nowWeek.setFirstDayOfWeek(Calendar.MONDAY);
		nowWeek.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		String nowStr = DateUtil.dateFormatStr(DateUtil.DATE_FORMAT, nowWeek.getTime());
		List<BfWeekmenu> weekMenuList = waresFrontDao.selectWeekMenu(nowStr);
		BfWeekmenu weekMenu = null;
		if(weekMenuList.size() > 0) {
			weekMenu = weekMenuList.get(0); 
		}
		return weekMenu;
	}

	/**
	 * 获取本周套餐列表
	 * @return
	 */
	@Override
	public List<BfWeekmenuCombo> selectWeekCombo() {
		Calendar nowWeek = Calendar.getInstance();
		nowWeek.setFirstDayOfWeek(Calendar.MONDAY);
		nowWeek.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		String nowStr = DateUtil.dateFormatStr(DateUtil.DATE_FORMAT, nowWeek.getTime());
		return waresFrontDao.selectWeekCombo(nowStr);
	}

	/**
	 * 获取当天套餐信息
	 * @return
	 */
	@Override
	public BfWeekmenuCombo selectTodayCombo() {
		//本周第一天
		Calendar nowWeek = Calendar.getInstance();
		nowWeek.setFirstDayOfWeek(Calendar.MONDAY);
		nowWeek.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		//今天
		Calendar today = Calendar.getInstance();
		String nowWeekStr = DateUtil.dateFormatStr(DateUtil.DATE_FORMAT, nowWeek.getTime());
		//当前是本周第几天
		int todayWeek = today.get(Calendar.DAY_OF_WEEK);
		if(1 == todayWeek) {
			todayWeek = 7;
		} else {
			todayWeek--;
		}
		BfWeekmenuCombo weekdishCombo = null;
		List<BfWeekmenuCombo> weekdishComboList = waresFrontDao.selectTodayCombo(nowWeekStr, todayWeek);
		if(weekdishComboList.size() > 0) {
			weekdishCombo = weekdishComboList.get(0);
		}
		return weekdishCombo;
	}

	/**
	 * 获取套餐菜品信息
	 * @param comboId	套餐Id
	 * @return
	 */
	@Override
	public List<BfComboDish> selectComboDish(int comboId) {
		List<BfComboDish> list = this.waresFrontDao.selectComboDish(comboId);
		return list;
	}
	
	/**
	 * 查询套餐中菜品字符串
	 * @param comboId	套餐Id
	 * @return [0]:中文菜名字符串 [1]:英文菜名字符串
	 */
	@Override
	public String[] selectComboDishStr(int comboId) {
		StringBuilder dishChStr = new StringBuilder();
		StringBuilder dishEnStr = new StringBuilder();
		List<BfComboDish> list = this.waresFrontDao.selectComboDishDetail(comboId);
		for (int i = 0; i < list.size(); i++) {
			BfComboDish comboDish = list.get(i);
			if(0 == i) {
				dishChStr.append(comboDish.getDish().getDishchname());
				dishEnStr.append(comboDish.getDish().getDishenname());
			} else {
				dishChStr.append("、").append(comboDish.getDish().getDishchname());
				dishEnStr.append("、").append(comboDish.getDish().getDishenname());
			}
		}
		String[] dishNameArray = new String[2];
		dishNameArray[0] = dishChStr.toString();
		dishNameArray[1] = dishEnStr.toString();
		return dishNameArray;
	}

	/**
	 * 根据类型查询商品信息
	 * @param typeId	类型Id
	 * @return
	 */
	@Override
	public List<BfDish> selectDishByType(int typeId) {
		List<BfDish> dishList = this.waresFrontDao.selectDishByType(typeId);
		return dishList;
	}

	/**
	 * 查询套餐信息
	 * @return
	 */
	@Override
	public List<BfCombo> selectCombo() {
		List<BfCombo> comboList = this.waresFrontDao.selectCombo();
		return comboList;
	}
	
	/**
	 * 查询商品详情
	 * @param dishId	商品Id
	 * @return
	 */
	@Override
	public BfDish selectDishById(int dishId) {
		BfDish dish = this.waresFrontDao.selectDishById(dishId);
		return dish;
	}
	
	/**
	 * 查询套餐详情
	 * @param comboId	套餐Id
	 * @return
	 */
	@Override
	public BfCombo selectComboById(int comboId) {
		BfCombo combo = this.waresFrontDao.selectComboById(comboId);
		return combo;
	}
}
